package com.six.controller;

import com.six.service.TableService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.io.IOException;

@RestController
@RequestMapping("/api/manager/tables")
@RequiredArgsConstructor
@PreAuthorize("hasRole('MANAGER')")
public class TableBatchController {

    private final TableService tableService;

    /**
     * POST /api/manager/tables/batch-qr
     * Body: { "prefix":"A", "from":1, "to":20 }
     */
    @PostMapping("/batch-qr")
    public void downloadBatchQr(@RequestParam Long storeId,
                                @RequestBody BatchGenDto dto,
                                HttpServletResponse resp) throws IOException, IOException {
        byte[] zipBytes = tableService.generateTablesWithQr(
                storeId,
                dto.getPrefix(),
                dto.getFrom(),
                dto.getTo()
        );
        resp.setContentType("application/zip");
        resp.setHeader("Content-Disposition",
                "attachment; filename=tables_" + storeId + ".zip");
        resp.getOutputStream().write(zipBytes);
    }

    @Data
    public static class BatchGenDto {
        @NotBlank
        private String prefix;
        @Min(1) private int from;
        @Min(1) private int to;
    }
}
